package com.xiaoshuidi.cloud.framework.kingdee.core.service;

import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import com.xiaoshuidi.cloud.framework.kingdee.config.KingdeeProperties;
import com.xiaoshuidi.cloud.framework.kingdee.core.dto.TransferConfirmDTO;
import com.xiaoshuidi.cloud.framework.kingdee.core.dto.TransferConfirmResp;
import com.xiaoshuidi.cloud.framework.kingdee.core.wsdl.confirm.WSSynTransferConfirmBillDataSrvProxy;
import com.xiaoshuidi.cloud.framework.kingdee.core.wsdl.confirm.WSSynTransferConfirmBillDataSrvProxyServiceLocator;
import com.xiaoshuidi.cloud.framework.kingdee.core.wsdl.login.EASLoginProxy;
import com.xiaoshuidi.cloud.framework.kingdee.core.wsdl.login.EASLoginProxyServiceLocator;
import com.xiaoshuidi.cloud.framework.kingdee.core.wsdl.login.WSContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.axis.client.Stub;
import org.springframework.util.StringUtils;

import javax.xml.rpc.ServiceException;
import java.rmi.RemoteException;

@Slf4j
@RequiredArgsConstructor
public class KingdeeServiceImpl implements KingdeeService {
    private final KingdeeProperties properties;

    @Override
    public TransferConfirmResp transfer(TransferConfirmDTO dto) {
        EASLoginProxy login;
        String sessionId;
        try {
            login = easLoginProxy(properties);
            WSContext wscontext = login.login(properties.getUserName(), properties.getPassword(), properties.getSlnName(), properties.getDcName(),
                    "L2", properties.getDbType(), properties.getAuthPattern());
            if (wscontext == null || !StringUtils.hasText(wscontext.getSessionId())) {
                throw new RuntimeException("金蝶登录失败");
            }
            sessionId = wscontext.getSessionId();
        } catch (Exception e) {
            log.error("[登录金蝶异常]", e);
            throw new RuntimeException("金蝶登录失败");
        }

        WSSynTransferConfirmBillDataSrvProxy wsSynTransferConfirmBillDataSrvProxy;
        try {
            wsSynTransferConfirmBillDataSrvProxy = wsSynTransferConfirmBillDataSrvProxy(properties);
            ((Stub) wsSynTransferConfirmBillDataSrvProxy).setHeader("http://login.webservice.bos.kingdee.com", "SessionId", sessionId);
            log.info("请求转账确认单 {} {}", sessionId, JsonUtils.toJsonString(dto));
            String resp = wsSynTransferConfirmBillDataSrvProxy.getData(JsonUtils.toJsonString(dto));
            log.info("请求转账确认单返回: {}", resp);
            TransferConfirmResp r = JsonUtils.parseObject2(resp, TransferConfirmResp.class);
            return r == null ? new TransferConfirmResp() : r;
        } catch (Exception e) {
            log.error("[提交转账确认单异常]", e);
            throw new RuntimeException("提交付款申请失败");
        } finally {
            logout(login);
        }
    }


    public void logout(EASLoginProxy login) {
        try {
            login.logout(properties.getUserName(), properties.getSlnName(), properties.getDcName(),
                    properties.getLanguage());
        } catch (RemoteException e) {
        }
    }

    public EASLoginProxy easLoginProxy(KingdeeProperties kingdeeProperty) throws ServiceException {
        EASLoginProxyServiceLocator locator = new EASLoginProxyServiceLocator();
        locator.setEASLoginAddress(kingdeeProperty.getServerAddr());
        return locator.getEASLogin();
    }

    public WSSynTransferConfirmBillDataSrvProxy wsSynTransferConfirmBillDataSrvProxy(KingdeeProperties kingdeeProperty) throws ServiceException {
        WSSynTransferConfirmBillDataSrvProxyServiceLocator locator = new WSSynTransferConfirmBillDataSrvProxyServiceLocator();
        locator.setWSSynTransferConfirmBillData_address(kingdeeProperty.getServerAddr());
        return locator.getWSSynTransferConfirmBillData();
    }
}
